//const api = require('../../../services/config/api')
import { request,requestlogin } from '../../../util'
const api = require('../../../api')
const filter = require('../../../utils/filter')
import Notify from '@vant/weapp/notify/notify';
import Toast from '@vant/weapp/toast/toast';

// pages/login/index.js
Page(
  filter.loginCheck({
    /**
     * 页面的初始数据
     */
    data: {
      formdata:{
        phone:"",
        yzmcode:"",
        id:0
      },
      display: 'none',
      display2: 'inline-black',
      seconds: 59,
      userInfo:wx.getStorageSync('userInfo')
    },

    async wxLogin(e) {
      var that = this
    },

    //绑定手机号
    bindphone(){
       if(this.data.formdata.phone==""|| this.data.formdata.phone==null || this.data.formdata.phone==undefined){
        Notify({ type: 'warning', message: '请输入手机号' });
          return false;
       }
       if(this.data.formdata.yzmcode==""|| this.data.formdata.yzmcode==null || this.data.formdata.yzmcode==undefined){
        Notify({ type: 'warning', message: '请输入验证码' });
        return false;
      }
      console.log(this.data.userInfo)
      this.setData({
        "formdata.id":this.data.userInfo.id
      })
      console.log(this.data.formdata)
      requestlogin(api.bindPhone,this.data.formdata,"post").then(function(res){
        console.log(res)
        if(res.code==200){
          Notify({ type: 'success', message: "操作成功" });
          wx.setStorageSync('bindphone', false)
          setTimeout(() => {
            wx.navigateTo({ url: '/pages/index/index' })
          }, 1000);
        }else{
          console.log(res.msg)
          Notify({ type: 'danger', message: res.msg });
        }
      })
      
    },
    getYzm(){
      //获取验证码
      var that = this
      if (
        this.data.formdata.phone == null ||
        this.data.formdata.phone.trim() == '' ||
        this.data.formdata.phone == undefined
      ) {
        Notify({ type: 'danger', message: '请填写手机号' })
        return false
      }
      Toast.loading({
        forbidClick: true,
        loadingType: 'spinner',
        duration: 0
      })
      this.setData({
        'formdata.isNotcreate': true
      })
      requestlogin(api.smsyzm + '?phone=' + this.data.formdata.phone + '&isNotcreate=' + true, {}, 'post').then(function (
        res
      ) {
        console.log(res)
        Toast.clear()
        if (res.code == 200) {
          Notify({ type: 'success', message: '发送成功' })
          that.setData({
            display: 'inline-black',
            display2: 'none'
          })
          const countDown = that.selectComponent('.control-count-down')
          countDown.start()
        } else {
          Notify({ type: 'danger', message: res.msg })
        }
      })
    },

    //键盘改变事件
    onChange(e){
       this.setData({
         'formdata.phone':e.detail
       })
    },
    onChange2(e){
      this.setData({
        'formdata.yzmcode':e.detail
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {}
  })
)
